package com.xtx.air.web.action.flight;

import com.xtx.air.bo.flight.IFlightBO;
import com.xtx.air.bo.role.IRoleBO;
import com.xtx.air.bo.user.IUserBO;
import com.xtx.air.dataobject.*;
import com.xtx.air.web.action.BaseAction;
import com.xtx.json.JsonArray;
import com.xtx.json.JsonObject;
import com.xtx.json.JsonTokener;
import com.xtx.util.StringUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User: hanyi
 * Date: 12-2-9
 * Time: 下午8:55
 */
public class FlightManageAction extends BaseAction{
    private IUserBO userBO;
    private IRoleBO roleBO;
    private FlightStorageDO query;
    private List<FlightDO> flightList;
    private IFlightBO flightBO;
    private FlightDO flight;
    private String storageJson;
    private List<FlightStorageDO> storageDOList;
    private Long fid;

    public String viewAdd(){
        return SUCCESS;
    }

    public String listMyFlight(){
        if(query == null){
            query = new FlightStorageDO();
        }
        UserDO user = userBO.getUserByEmail(getOperName());
        if(user != null){
            query.setCreator_id(user.getId());
            flightList = flightBO.findFlightByStorageCreator(query);
        }
        return SUCCESS;
    }

    public String add() {
        try {
            UserDO userDO = userBO.getUserByEmail(getOperName());
            FlightDO flightDO = flightBO.getFlight(flight.getFlight_num(), flight.getStart_city(), flight.getEnd_city());
            Long fid = null;
            if (flightDO == null) {
                flight.setCreator_id(userDO.getId());
                fid = flight.getFid();
            } else {
                fid = flightDO.getFid();
            }
            storageDOList = new ArrayList<FlightStorageDO>();
            JsonArray ja = new JsonArray(new JsonTokener(storageJson));
            for (JsonObject jo : (Collection<JsonObject>) ja) {
                FlightStorageDO storageDO = convertJsonToStorageDO(jo);
                storageDO.setFid(fid);
                storageDO.setCreator_id(userDO.getId());
                storageDOList.add(storageDO);
            }
            if(flightDO == null){
                flightBO.addFlightAndStorage(flight,storageDOList);
            }else {
                flightBO.addFlightStorage(storageDOList);
            }
            addActionMessage("添加机票成功!");
        } catch (Exception e) {
            addActionError("添加机票信息失败："+e.getMessage());
            logger.error("添加航班信息失败", e);
        }
        return SUCCESS;
    }

    public String viewUpdate(){
        flight = flightBO.getFlight(fid,null);
        storageDOList = flight.getStorages();
        return SUCCESS;
    }

    public String update(){
        try {
            UserDO userDO = userBO.getUserByEmail(getOperName());
            flightBO.updateFlight(flight);
            storageDOList = new ArrayList<FlightStorageDO>();
            JsonArray ja = new JsonArray(new JsonTokener(storageJson));
            for (JsonObject jo : (Collection<JsonObject>) ja) {
                FlightStorageDO storageDO = convertJsonToStorageDO(jo);
                storageDO.setCreator_id(userDO.getId());
                storageDO.setFid(flight.getFid());
                storageDOList.add(storageDO);
            }
            flightBO.updateStorageByFid(flight.getFid(),storageDOList);
            addActionMessage("修改机票成功!");
        } catch (Exception e) {
            addActionError("修改机票信息失败："+e.getMessage());
            logger.error("修改航班信息失败", e);
        }
        return SUCCESS;
    }

    public FlightStorageDO convertJsonToStorageDO(JsonObject jo){
        FlightStorageDO flightStorageDO = new FlightStorageDO();
        if (StringUtil.isNotEmpty((String) jo.get("classType"))) {
            flightStorageDO.setClass_type((String) jo.get("classType"));
        }
        if (StringUtil.isNotEmpty((String) jo.get("boarding_date"))) {
            flightStorageDO.setBoarding_date((String) jo.get("boarding_date"));
        }
        if (StringUtil.isNotEmpty((String) jo.get("discount"))) {
            flightStorageDO.setDiscount((String) jo.get("discount"));
        }
        if (StringUtil.isNotEmpty((String) jo.get("spec_discount"))) {
            flightStorageDO.setSpec_discount((String) jo.get("spec_discount"));
        }
        if (StringUtil.isNotEmpty((String) jo.get("current_count"))) {
            flightStorageDO.setCurrent_count(Long.parseLong((String) jo.get("current_count")));
        }
        return flightStorageDO;
    }

    public IUserBO getUserBO() {
        return userBO;
    }

    public void setUserBO(IUserBO userBO) {
        this.userBO = userBO;
    }

    public IRoleBO getRoleBO() {
        return roleBO;
    }

    public void setRoleBO(IRoleBO roleBO) {
        this.roleBO = roleBO;
    }

    public FlightStorageDO getQuery() {
        return query;
    }

    public void setQuery(FlightStorageDO query) {
        this.query = query;
    }

    public List<FlightDO> getFlightList() {
        return flightList;
    }

    public void setFlightList(List<FlightDO> flightList) {
        this.flightList = flightList;
    }

    public IFlightBO getFlightBO() {
        return flightBO;
    }

    public void setFlightBO(IFlightBO flightBO) {
        this.flightBO = flightBO;
    }

    public FlightDO getFlight() {
        return flight;
    }

    public void setFlight(FlightDO flight) {
        this.flight = flight;
    }

    public String getStorageJson() {
        return storageJson;
    }

    public void setStorageJson(String storageJson) {
        this.storageJson = storageJson;
    }

    public List<FlightStorageDO> getStorageDOList() {
        return storageDOList;
    }

    public void setStorageDOList(List<FlightStorageDO> storageDOList) {
        this.storageDOList = storageDOList;
    }

    public Long getFid() {
        return fid;
    }

    public void setFid(Long fid) {
        this.fid = fid;
    }
}
